<?php
/**
 * 个人资料管理
 * 
 * @version $Id$
 */

class Module_useredit
{
	/**
	 * 构造函数
	 */
	function __construct() {
		
	}
	
	/**
	 * 默认 action
	 */
	function index() {
		
	}

	/**
	 * 编辑个人资料
	 */
	function edit() {
		$uin = SessionInfo('uin');
		
		$smarty = loadSmarty();
		
		// 获取用户数据
		loadLib('User');
		$userinfo = USER::getFullInfo($uin);
		$smarty->assign('userinfo' , $userinfo);
		
		return $smarty->fetch('useredit/edit.tpl.php');
	}
	
	/**
	 * 编辑个人资料
	 */
	function doedit() {
		$name = isset($_POST['name']) ? trim($_POST['name']) : null;
		$org_name = isset($_POST['org_name']) ? trim($_POST['org_name']) : null;
		$sex = isset($_POST['sex']) ? intval($_POST['sex']) : 0;
		$org_sex = isset($_POST['org_sex']) ? intval($_POST['org_sex']) : 0;
		$intro = isset($_POST['intro']) ? trim($_POST['intro']) : '';
		$org_intro = isset($_POST['org_intro']) ? trim($_POST['org_intro']) : '';
		
		// 检查姓名有效性
		if($name === null || strlen($name) == 0) {
			return View::error('没有填写姓名');
		}
		
		if(strlen($intro) > 500) {
			return View::error('个人介绍文字太多，超过了限制');
		}
		
		$uin = SessionInfo('uin');
		
		// 判断是否有需要更新的字段
		$key_array = array('name' , 'sex' , 'intro');
		$update_array = array();
		foreach($key_array as $key) {
			$org_key = 'org_' . $key;
			if($$org_key != $$key) {
				$update_array[$key] = $$key;
			}
		}
		
		loadLib('User');
		$code = User::updateInfo($uin , $update_array);
		
		if($code === 0) {
			loadLib('Operate');
			Operate::getAdd($uin , 0 , 'user_edit');
			return View::success('修改资料成功');
		} else {
			return View::error('修改资料失败，请稍后再试' , $code);
		}
	}
	
	/**
	 * 编辑密码
	 */
	function editpassword() {
		$uin = SessionInfo('uin');
		
		$smarty = loadSmarty();
		
		// 获取用户数据
		loadLib('User');
		$password = USER::getPassword($uin);
		$smarty->assign('password' , $password);
		
		return $smarty->fetch('useredit/editpassword.tpl.php');
	}
	
	/**
	 * 编辑密码
	 */
	function doeditpassword() {
		$password = isset($_POST['password']) ? intval($_POST['password']) : null;
		$cpassword = isset($_POST['cpassword']) ? intval($_POST['cpassword']) : null;
		
		// 检查密码有效性
		if($password === null || strlen($password) <= 1) {
			return View::error('没有填写密码或密码长度太短');
		}
		
		// 密码长度校验
		if(strlen($password) > 6) {
			return View::error('密码超过了长度限制，最长是6个数字');
		}
		
		// 两次输入的密码校验
		if($cpassword !== $password) {
			return View::error('两次输入的密码不一致');
		}
		
		$uin = SessionInfo('uin');
		
		// 判断是否有需要更新的字段
		$key_array = array('password');
		$update_array = array(
			'password' => $password,
		);
		
		loadLib('User');
		$code = User::updateInfo($uin , $update_array);
		
		if($code === 0) {
			loadLib('Operate');
			Operate::getAdd($uin , 0 , 'user_pwd');
			return View::success('修改密码成功');
		} else {
			return View::error('修改密码失败，请稍后再试' , $code);
		}
	}
	/**
	 * 编辑个人头像页面
	 */
	function upload(){
		$smarty = loadSmarty();
		$uin = SessionInfo('uin');
		// 获取用户数据
		loadLib('User');
		$userinfo = USER::getFullInfo($uin);
		$user_logo = User::getLogo($userinfo , 60);
		
		$smarty->assign('user_logo' , $user_logo);
		$smarty->assign('uin',$uin);
		$smarty->assign('userinfo',$userinfo);
		return $smarty->fetch('useredit/upload.tpl.php');
	}
	//上传头像页面
	function photos() {
		$uptypes=array('image/jpg',  //上传文件类型列表
		'image/jpeg',
		'image/png',
		'image/pjpeg',
		'image/gif',
		'image/bmp',
		'application/x-shockwave-flash',
		'image/x-png');
		
		$max_file_size = 5000000;   //上传文件大小限制, 单位BYTE
		$width = 30;
		$height = 30;
		$upimg_width = 60;
		$upimg_height = 60;
		
		if ($_SERVER['REQUEST_METHOD'] == 'POST')
		{

			//是否存在文件
			if(!empty($_FILES)){
				if (!is_uploaded_file($_FILES["upfile"]["tmp_name"])){
					return View::error('没有选择要上传的照片或文件不存在！');
				}
			}
			
			$file = isset($_FILES["upfile"]) ? $_FILES["upfile"] : null;
		
			if($max_file_size < $file["size"])
			//检查文件大小
			{
				return View::error('文件太大！');
			 }
			
			if(!in_array($file["type"], $uptypes))
			//检查文件类型
			{
				return View::error('只能上传图像文件或Flash！');
			}
			$uin = SessionInfo('uin');	
			loadLib('User');
			$destination_folder = User::getPath($uin); //上传文件路径
			$destination_folder = ROOT_PATH . '/web' . $destination_folder;
			
			$filename = $file['tmp_name'];
			$image_size = getimagesize($filename);
			
			$image_30 = $uin . '_30.jpg'; //图片名字
			$image_60 = $uin . '_60.jpg'; //图片名字
			$image_30_path = $destination_folder.$image_30;
			$image_60_path = $destination_folder.$image_60;

		//print_r($img_path);
			// 生成图片
			loadLib('Thumb');
			$img_30_info = Thumb::genarate($filename , $destination_folder , $image_30 , $width , $height);
			$img_60_info = Thumb::genarate($filename , $destination_folder , $image_60 , $upimg_width , $upimg_height);
				
			if(!file_exists($image_30_path) || empty($img_30_info) || !file_exists($image_60_path) || empty($img_60_info)) {
				return View::error('图片上传失败');
			}
			//上传图片
			$logo_state = 2;
			$update_array = array(
				'logo_state' => $logo_state,
			);
			$code = User::updateBaseInfo($uin , $update_array);
			if($code === 0) {
				loadLib('Operate');
				Operate::getAdd($uin , 0 , 'user_upload');		
				header('Location: ' . SITEURL . '/?m=profile&uin='. $uin .'&sid=' . sessionInfo('skey') );
			} else {
				return View::error('系统繁忙，请稍后再试' , $code);
			}
		}
	}
}

# end of script
